<?php

namespace App\Http\Controllers\Admin;

use App\Model\Reply;
use App\Model\Topic;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\TopicRequest;

class TopicsController extends Controller
{
    public function __construct()
    {
//        $this->middleware('auth', ['except' => ['index', 'show']]);
    }

	public function index()
	{
		$topics = Topic::paginate(15);
		return view('admin.topics.index', compact('topics'));
	}

    public function show(Topic $topic)
    {
        $replies = $topic->replies()->with('user')->get();
        return view('admin.topics.show', compact('topic','replies'));
    }

	public function destroy(Topic $topic)
	{
//		$this->authorize('destroy', $topic);
		$data = $topic->delete();

        if ($data){
            return $this->success('删除成功');
        }else {
            return $this->error('删除失败');
        }
	}

	public function search(Request $request)
    {
        $keyword = $request->input('keywords');
        $data = Topic::where('title','like','%'.$keyword.'%')->paginate(15);
        return view('admin.topics.search',compact('data'));
    }
}